//
//  RootMore.m
//  Trading365
//
//  Created by yifei tong on 11-12-15.
//  Copyright (c) 2011年 dhc. All rights reserved.
//

#import "RootMore.h"
#import "LoginView.h"
#import "AboutView.h"
#import "MoreFeedBack.h"
#import "MoreChangePosi.h"
#import "TaskManager.h"
#import "XmlRpc.h"
#import "DHCUtil.h"
#import "AppDelegate.h"
#import "HttpServerURL.h"
#import "MoreTestView.h"
#import "InfoType.h"

@implementation RootMore
@synthesize table;
@synthesize InfoListTitle_Login,InfoListTitle_Logout;
@synthesize nickName,posiName;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc
{
    [table release];
    [InfoListTitle_Login release];
    [InfoListTitle_Logout release];
    [nickName release];
    [posiName release];
    [super dealloc];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"更多";
    [self.view setBackgroundColor:BACKGROUND_COLOR];
    self.navigationController.navigationBar.tintColor = NAVIGATION_BAR_COLOR;

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate setMoreViewCtl:self];

    UIImage* imageBar = [UIImage imageNamed:PIC_MAIL_TOP_BAR];
    [self.navigationController setNavigationBarHidden:NO];

    NSString* devVersion = [[UIDevice currentDevice] systemVersion];
    if ([devVersion compare:@"4.3.3"] == 0)
    {
       [self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)];
        [self.navigationController.navigationBar setTintColor:NAVIGATION_BAR_COLOR];
    }else
    {
        [self.navigationController.navigationBar setBackgroundImage:imageBar forBarMetrics:UIBarMetricsDefault];
    }

    NSArray *temp_array2=[[NSArray alloc]initWithObjects: @"职位切换",@"用户切换",@"意见反馈",@"关于",nil];//@"注销帐户"
    self.InfoListTitle_Logout=temp_array2;
    [temp_array2 release];
        
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    ///
    nickName=[[[Session share] getMember] name_cn];
    posiName=[[[Session share] getMember] posi_name];
    
    [self.table reloadData];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - table view

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.InfoListTitle_Logout count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
                                       reuseIdentifier: CellIdentifier] autorelease];
    }
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
    
    NSUInteger row = indexPath.row;

    if (row < [self.InfoListTitle_Logout count]) 
    {
        cell.textLabel.text = [self.InfoListTitle_Logout objectAtIndex:row];
    }
        
    if (row == 0){
        cell.detailTextLabel.text =posiName;
    }

    if (row == 1){
        cell.detailTextLabel.text =nickName;
    }
    
    cell.detailTextLabel.textAlignment = UITextAlignmentRight;

    if (row == 2 || row == 3){
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = indexPath.row;
    if (row == 0) {
        MoreChangePosi *alert = [[MoreChangePosi alloc] initWithTitle:@"职位切换"    
                                                                    message:@"请选择您要转换的职位:"    
                                                                   delegate:self
                                                          cancelButtonTitle:@"OK"
                                                          otherButtonTitles: nil];
        [alert setBackgroundColor:[UIColor clearColor]];
        [alert show];
        [alert release];
    }

    //Logout
    if (row == 1) {
        //帐户注销
//        UIActionSheet *styleAlert = [[UIActionSheet alloc] 
//                                     initWithTitle:@"请选择退出类型"
//                                            delegate:(id)self 
//                                   cancelButtonTitle:@"取  消"                              destructiveButtonTitle:@"清除登陆信息退出"
//                                   otherButtonTitles: @"重新登陆",
//                                     @"暂时退出",
//                                     nil];
//        
//        [styleAlert showFromTabBar:self.tabBarController.tabBar];
//        [styleAlert release];
        
        //用户切换
        //跳转到登陆界面
        LoginView *loginview = [[LoginView alloc] init];
        [loginview setFlagLoginType:INFO_TYPE_LOGIN_BLANK];//INFO_TYPE_LOGIN_AUTO
        
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        [loginview.view setFrame:CGRectMake(0, 0, 320, 480)];
        [appDelegate.window addSubview:loginview.view];
        [loginview release];
        
        [self.table reloadData];

    }

//    if (row == 3) {
//        SettingList *setting = [[SettingList alloc] init];
//        [self.navigationController pushViewController:setting animated:YES];
//        [setting release];
//    }
    
    //意见反馈
    if (row == 2){
        MoreFeedBack *feedback = [[MoreFeedBack alloc] init];
        [self.navigationController pushViewController:feedback animated:YES];
        [feedback release];
    }
    
    //关于
    if (row == 3) {
        AboutView *aboutview = [[AboutView alloc] init];
        [self.navigationController pushViewController:aboutview animated:YES];
        [aboutview release];
        
//        MoreTestView *testview = [[MoreTestView alloc] init];
//        [self.navigationController pushViewController:testview animated:YES];
//        [testview release];

    }

//    //Test
//    if (row == 4) {
//        MoreTestView *testview = [[MoreTestView alloc] init];
//        [self.navigationController pushViewController:testview animated:YES];
//        [testview release];
//    }

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [[Session share] printMember];

    nickName=[[[Session share] getMember] name_cn];
    posiName=[[[Session share] getMember] posi_name];

    [self.table reloadData];
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)modalView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //NSString *tmp=@"";
    switch (buttonIndex){
         case 0:
        {
            //tmp=@"清除登陆信息退出";
            NSMutableDictionary *userinfo=[[NSMutableDictionary alloc] initWithCapacity:5];
            [userinfo setValue:@"" forKey:Key_SaveLogin_UserID];
            [userinfo setValue:@"" forKey:Key_SaveLogin_UserName];
            [userinfo setValue:@"" forKey:Key_SaveLogin_NickName];
            [userinfo setValue:@"" forKey:Key_SaveLogin_RoleID];
            [userinfo setValue:@"" forKey:Key_SaveLogin_RoleName];
            [userinfo setValue:@"" forKey:Key_SaveLogin_PosiID];
            [userinfo setValue:@"" forKey:Key_SaveLogin_PosiName];
            
            [[Session share] setMember:userinfo];
            [[Session share] setLoginFlag:NO];
            
            [userinfo writeToFile:[DHCUtil dataFilePath] atomically:YES]; 
            
            exit(0);
            break;
        }
        case 1:
        {
            //tmp=@"重新登陆";
            //跳转到登陆界面
            LoginView *loginview = [[LoginView alloc] init];
            [loginview setFlagLoginType:INFO_TYPE_LOGIN_BLANK];//INFO_TYPE_LOGIN_AUTO
            
            AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            [loginview.view setFrame:CGRectMake(0, 0, 320, 480)];
            [appDelegate.window addSubview:loginview.view];
            [loginview release];
            
            [self.table reloadData];

            break;
        }
        case 2:
        {
            //tmp=@"暂时退出";
            exit(0);
            break;
        }
        case 3:
        {
            //tmp=@"取消";
            break;
        }
        default:
        {
            //tmp=[NSString stringWithString:@"Cancel"];
            break;
        }
    }
    
}

#pragma mark - my fun
//
-(void)refreshData
{
    nickName=[[[Session share] getMember] name_cn];
    posiName=[[[Session share] getMember] posi_name];
    
    [self.table reloadData];    
}

@end
